class Solution 
{
public:
    int triangleNumber(vector<int>& nums) 
    {
        // 优化
        sort(nums.begin(), nums.end());
        
        int ans = 0, end = nums.size() - 1;
        for(int i = end; i >= 2; --i)
        {
            int left = 0, right = i - 1;
            while(left < right)
            {
                if(nums[left] + nums[right] > nums[i])
                {
                    ans += right - left;
                    --right;
                }
                else ++left;
            }
        }

        return ans;
    }
};